<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" isELIgnored="false" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<form>
    <tabel border=1 bgcolor="lightblue" align="center">
        <tr>
            <td>姓名：</td>
            <td>
                <input id="uname" class="textSize" type="text" name="uname" value="${uname}"/>
            </td>
        </tr>
        <br>
        <tr>
            <td>密码：</td>
            <td>
                <input id="upwd" class="textSize" type="password" maxlength="20" name="upwd"/>
            </td>
        </tr>
        <br>
        <tr>
            <td>确认密码：</td>
            <td>
                <input id="cpwd" class="textSize" type="password" maxlength="20" name="confirmPwd"/>
                <span id="error"></span>
            </td>
        </tr>
        <br>
        <tr>
            <td colspan="2" align="center">
                <input type="button" value="注册" onclick="goR()"/>
                <br>
                <span id="msg"></span>
            </td>
        </tr>
    </tabel>
</form>

<script type="text/javascript">

    $(function () {
        $("#cpwd").blur(function () {
            const uname = $("#uname").val();
            const upwd = $("#upwd").val();
            const cpwd = $("#cpwd").val();
            if (upwd !== cpwd) {
                $("#error").html("两次输入的密码不一致").css("color", "red");
            }
        }).focus(function () {    //获取焦点事件
            $(this).nextAll("span").text("");
        })
    });


    function goR() {
        $.post({
            url: "${pageContext.request.contextPath}/user/reg",
            data: {
                "uname": $("#uname").val(),
                "upwd": $("#upwd").val(),
                "confirmPwd": $("#cpwd").val()
            },
            success(data) {
                console.log(data);
                $("#msg").html(data);
                if (data === "注册成功") {
                    window.location.href = "${pageContext.request.contextPath}/user/toLogin";
                }
            }
        });
    }
</script>
</body>
</html>